{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "944e3cdb-ee53-4439-9305-c8a2a4f2bb46",
   "metadata": {},
   "source": [
    "# Parallel Computing"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "286b7c4e-88e3-484b-a866-6016fead2368",
   "metadata": {},
   "source": [
    "## 一、多线程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "50fb2ad7-4725-41dc-8edf-63b173571cb7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4-element Vector{Int64}:\n",
       " 2\n",
       " 3\n",
       " 4\n",
       " 5"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "using Distributed\n",
    "addprocs(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "98e35f9b-e093-40f4-9a0b-8b056b6a3774",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "现在有4个线程\n"
     ]
    }
   ],
   "source": [
    "println(\"现在有$(nprocs()-1)个线程\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "619f9f03-2479-4cb6-a788-f8d83671caf1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 finish\n",
      "2 finish\n",
      "3 finish\n",
      "4 finish\n",
      "5 finish\n",
      "6 finish\n",
      "7 finish\n",
      "8 finish\n",
      "9 finish\n",
      "10 finish\n",
      "11 finish\n",
      "12 finish\n",
      "13 finish\n",
      "14 finish\n",
      "15 finish\n",
      "16 finish\n",
      "17 finish\n",
      "18 finish\n",
      "19 finish\n",
      "20 finish\n",
      "21 finish\n",
      "22 finish\n",
      "23 finish\n",
      "24 finish\n",
      "25 finish\n",
      "26 finish\n",
      "27 finish\n"
     ]
    },
    {
     "ename": "LoadError",
     "evalue": "InterruptException:",
     "output_type": "error",
     "traceback": [
      "InterruptException:",
      "",
      "Stacktrace:",
      " [1] try_yieldto(undo::typeof(Base.ensure_rescheduled))",
      "   @ Base .\\task.jl:705",
      " [2] wait",
      "   @ .\\task.jl:764 [inlined]",
      " [3] wait(c::Base.GenericCondition{Base.Threads.SpinLock})",
      "   @ Base .\\condition.jl:106",
      " [4] _wait(t::Task)",
      "   @ Base .\\task.jl:288",
      " [5] wait",
      "   @ .\\task.jl:315 [inlined]",
      " [6] threading_run(func::Function)",
      "   @ Base.Threads .\\threadingconstructs.jl:34",
      " [7] top-level scope",
      "   @ .\\threadingconstructs.jl:93",
      " [8] eval",
      "   @ .\\boot.jl:360 [inlined]",
      " [9] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)",
      "   @ Base .\\loading.jl:1094"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "28 finish\n",
      "29 finish\n",
      "30 finish\n",
      "31 finish\n",
      "32 finish\n",
      "33 finish\n",
      "34 finish\n",
      "35 finish\n",
      "36 finish\n",
      "37 finish\n",
      "38 finish\n",
      "39 finish\n",
      "40 finish\n"
     ]
    }
   ],
   "source": [
    "Threads.@threads for i = 1:8\n",
    "    sleep(rand())\n",
    "    println(\"$i finish\")\n",
    "end"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.6.0",
   "language": "julia",
   "name": "julia-1.6"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
